<?php

require('config.php');

$page = new rootLayoutPage();
$page->addJavaScript('jquery.validate.js');
$page->addJavaScript('jquery.inscrioption.js');
$page->setBaliseTitle('profil_modifier_balise');
$page->setDescription('profil_modifier_description');

$translate = tmxTranslate('profil-modifier.xml', REP_XLAT_TEMPLATE);
$menu = new layoutMenuLeft();
$menuPage = new layoutMenuPage();
$menu->pushMenu($menuPage);
$page->setMenuPage($menu);

if(!isset($_SESSION['id_user'])) {
    $msg = new Message($translate['text_message_1'], Message::AVERT);
    Persistence::register(PERSIST_MESSAGE, $msg);
    redirectFront('connexion');
}



$user = modelUser::getProfil($_SESSION['id_user']);

//$nickname = modelUser::getProfilByIdInfos($id_infos);

///test access right
//if($nickname->id_user!=$_SESSION['id_user'] && $nickname->id_infos){
//	redirectFront('index');
//}

//if($id_infos<=0 && $id_infos==''){
	$login = modelUser::getIdUser($_SESSION['id_user'])->login;
//}
 
if(isset($_POST['modify'])){
	try {
	//$email_mail = addslashes($_POST['login_user']);
	$nom = addslashes($_POST['nom']);
    $prenom = addslashes($_POST['prenom']);
	$city = addslashes($_POST['city']);
	$id_country = addslashes($_POST['id_country']);
	//$phone_number = addslashes($_POST['phone_number']);
	$i_am = addslashes($_POST['i_am']);
	$id_titre = addslashes($_POST['id_titre']);

	$check_mail = isset($_POST['ch_email'])?1:0;
	$check_name = isset($_POST['ch_name'])?1:0;
	$check_city = isset($_POST['ch_city'])?1:0;
	$check_country = isset($_POST['ch_country'])?1:0;
	$check_phone = isset($_POST['ch_phone'])?1:0;

	$agency = addslashes($_POST['agency']);
	$website = addslashes($_POST['website']);
	
	
	
	$id_user_info = $user->id?$user->id:$_SESSION['id_user'];
	$listPhones = modelUser::getListCountryLanguage();
		if($user){
			modelUser::updateProfile($id_user_info,$nom,$prenom,$city,$id_country,$i_am,$check_mail,$check_name,$check_city,$check_country,$id_titre,$website,$agency);
			modelUser::deleteUserPhone($id_user_info);
			for($i=1;$i<$_POST['max_phone']+1;$i++){
				$phone = addslashes($_POST[$i.'-phone_number']);
				//$checkPhone = isset($_POST[$i.'-ch_phone'])?1:0;
				$id_phone_language = '';
				foreach($listPhones as $kp => $phones){
					$var = '-check_phones'.$phones->id_country_language;
					 if(isset($_POST[$i.$var])){
					 	$id_phone_language .= $phones->id_country_language.',';
					 }
				}
				modelUser::insertPhoneLanguageUser($id_user_info, $phone,substr($id_phone_language, 0, -1),1);
			}
		}else{
			$id_infos = modelUser::insertUserInfoFront($id_user_info,$nom,$prenom,$city,$id_country,$i_am,$check_mail,$check_name,$check_city,$check_country,$id_titre);
			modelUser::deleteUserPhone($id_infos);
			for($i=1;$i<$_POST['max_phone']+1;$i++){
				$phone = addslashes($_POST[$i.'-phone_number']);
				//$checkPhone = isset($_POST[$i.'-ch_phone'])?1:0;
				$id_phone_language = '';
				foreach($listPhones as $kp => $phones){
					$var = '-check_phones'.$phones->id_country_language;
					 if(isset($_POST[$i.$var])){
					 	$id_phone_language .= $phones->id_country_language.',';
					 }
				}
				modelUser::insertPhoneLanguageUser($id_infos, $phone,substr($id_phone_language, 0, -1),1);
			}
		}
		$msg = new Message($translate['text_message_2'], Message::INFO);
    	Persistence::register(PERSIST_MESSAGE, $msg);
		redirectFront('profil');
    }
    catch(Exception $e) {
        $msg = new Message($e->getMessage(), Message::AVERT);
        Persistence::register(PERSIST_MESSAGE, $msg);
    }
}



//-- corps de la page --------------------------------------------------------//
$tpl_file='profil-modifier.tpl';
$profil_modifier = new flyLayout(REP_TPL . $tpl_file);
$profil_modifier->start();

//$profil_modifier->setVariable('rep_ajax', REP_AJAX);
//$tabs = new layoutTabs();
//$tabs->pushLink('Mon compte', '/profil.php');
//$tabs->pushLink('Mes messages', '/profil-message.php');
//$profil_modifier_modifier->includeLayout('tabs', $tabs);


	$profil_modifier->setVariable('nom', $user->nom);
	$profil_modifier->setVariable('prenom', $user->prenom);
	$profil_modifier->setVariable('city', $user->city);
	$profil_modifier->setVariable('email', $login);

	$profil_modifier->setVariable('email_check', $user->public_email?'checked="checked"':'');
	$profil_modifier->setVariable('name_check', $user->public_name?'checked="checked"':'');
	$profil_modifier->setVariable('city_check', $user->public_city?'checked="checked"':'');
	$profil_modifier->setVariable('country_check', $user->public_country?'checked="checked"':'');
	//die($user->public_country."=============================================================================");
	//$profil_modifier->setVariable('agency_check', $user->public_city?'checked="checked"':'');
	//$profil_modifier->setVariable('website_check', $user->public_country?'checked="checked"':'');
	
	$profil_modifier->setVariable('agency', $user->agency_name);
	$profil_modifier->setVariable('website', $user->website);
	

	$listCountry = modelUser::getListCountry();
	foreach($listCountry as $kk => $country){
		$name = 'nom_pays_'.$_SESSION['language_code'];
		$profil_modifier->setVariable('selection',$country->id_pays == $user->id_country?'selected="selected"':'');
		$profil_modifier->setVariable('id_country',$country->id_pays);
		$profil_modifier->setVariable('name_country',$country->$name);
		$profil_modifier->parseList('list_country');
	
	}
	$listPhone = modelUser::getListUserPhoneNumber($user->id);
	if($listPhone->size() > 0){
		foreach($listPhone as $kkk => $phone){
			//$profil_modifier->setVariable('phone_check', $phone->public?'checked="checked"':'');
			$profil_modifier->setVariable('phone_number', $phone->phone_number);
			$profil_modifier->setVariable('index', $kkk+1);
			$profil_modifier->setVariable('max_size_phone', $listPhone->size());
			$listPhoneLanguage = modelUser::getListLanguageSpeakerByID($phone->id_phone_number_language);
			$profil_modifier->resetList('list_phone_language');
			foreach($listPhoneLanguage as $k => $phoneLanguage){
				$phone_name = 'language_'.$_SESSION['language_code'];
				$profil_modifier->setVariable('name_phone_language',$phoneLanguage->$phone_name);
				$profil_modifier->setVariable('id_phone_language',$phoneLanguage->id_country_language);
				$profil_modifier->setVariable('check_phone_speaker','checked="ture"');
				$profil_modifier->parseList('list_phone_language');
			}
			
			if($kkk==0){
				//$profil_modifier->showBlock('first_p');
				$profil_modifier->hideBlock('second_p');
			}else{
				$profil_modifier->showBlock('second_p');
				//$profil_modifier->hideBlock('first_p');
			}
			$profil_modifier->parseList('list_phone');
		}
	}else{
		$profil_modifier->setVariable('index', 1);
		$profil_modifier->setVariable('max_size_phone', 1);
		$listPhone = modelUser::getListPhoneLanguageDefaulf(LANGUAGE_PHONE_SPEAK_DEFAULT);
		foreach($listPhone as $k => $phone){
			$phone_name = language_.$_SESSION['language_code'];
			$profil_modifier->setVariable('name_phone_language',$phone->$phone_name);
			$profil_modifier->setVariable('id_phone_language',$phone->id_country_language);
			$profil_modifier->parseList('list_phone_language');
		}
		$profil_modifier->showBlock('first_p');
		$profil_modifier->parseList('list_phone');
	}
	$listIam = modelUser::getListIam();
	foreach($listIam as $k => $iAm){
		$name = 'i_am_'.$_SESSION['language_code'];
		
		$profil_modifier->setVariable('checked',$iAm->id_i_am == $user->id_i_am?'CHECKED="CHECKED"':'');
		
		$profil_modifier->setVariable('id_i_am',$iAm->id_i_am);
		$profil_modifier->setVariable('name_i_am',$iAm->$name);
		$profil_modifier->parseList('i_am');
	}

	$listtitre = modelUser::getListTitre();
	foreach($listtitre as $k => $titre){
		$nameTitre = 'titre_court_'.$_SESSION['language_code'];
		$profil_modifier->setVariable('checked_titre',$titre->id_titre == $user->id_titre?'CHECKED="CHECKED"':'');
		$profil_modifier->setVariable('id_titre',$titre->id_titre);
		$profil_modifier->setVariable('name_titre',$titre->$nameTitre);
		$profil_modifier->parseList('titre');
	}


$profil_modifier->stop();
$page->setBody($profil_modifier);
$page->display();

?>